package com.test.myfreemark;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class MyModel {
	private String preFix;//前缀
	private String sufFix;//后缀名
	private String detailPackge;//细包
	private String packageName;//包名
	private String templateName;//模板名
	private List <Map<String,String>> property;//装实体类属性以及类型和getset方法名以及数据库字段名集合。
	private String beanName;//实体类名。
	private String varName;//变量名。
	private String tableName;//表名。
	private String nameSpance;//mapper命名空间
	private List <String>importBao = new ArrayList<String>();//需要倒的包集合
	
	public String getVarName() {
		return varName;
	}
	public void setVarName(String varName) {
		this.varName = varName;
	}
	public String getDetailPackge() {
		return detailPackge;
	}
	public void setDetailPackge(String detailPackge) {
		this.detailPackge = detailPackge;
	}
	public List<String> getImportBao() {
		return importBao;
	}
	public void setImportBao(List<String> importBao) {
		this.importBao = importBao;
	}
	public String getNameSpance() {
		return nameSpance;
	}
	public void setNameSpance(String nameSpance) {
		this.nameSpance = nameSpance;
	}
	public String getTableName() {
		return tableName;
	}
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}
	public List<Map<String, String>> getProperty() {
		return property;
	}
	public void setProperty(List<Map<String, String>> property) {
		this.property = property;
	}
	public String getBeanName() {
		return beanName;
	}
	public void setBeanName(String beanName) {
		this.beanName = beanName;
	}
	//装getset方法后面转成大写的集合
	public String getPreFix() {
		return preFix;
	}
	public void setPreFix(String preFix) {
		this.preFix = preFix;
	}
	public String getSufFix() {
		return sufFix;
	}
	public void setSufFix(String sufFix) {
		this.sufFix = sufFix;
	}
	public String getPackageName() {
		return packageName;
	}
	public void setPackageName(String packageName) {
		this.packageName = packageName;
	}
	public String getTemplateName() {
		return templateName;
	}
	public void setTemplateName(String templateName) {
		this.templateName = templateName;
	}
	//根据文件类型创建MyModel对象设置基本信息
	public static MyModel getModel(String FileType){
		MyModel model = new MyModel();
		switch (FileType) {
		case "BEAN":
			model.setTemplateName("Bean.ftl");
			model.setPreFix("");
			model.setSufFix(".java");
			model.setPackageName(".model");
			break;
		case "DAO":
			model.setTemplateName("Dao.ftl");
			model.setPreFix("Dao");
			model.setSufFix(".java");
			model.setPackageName(".dao");
			model.getImportBao().add("import java.util.List");
			model.getImportBao().add("import org.apache.ibatis.annotations.Param");
			model.getImportBao().add("import com.renbang.model.common.Page");
			break;
		case "MAPPER":
			model.setTemplateName("Mapper.ftl");
			model.setPreFix("Mapper");
			model.setSufFix(".xml");
			model.setPackageName(".dao");
			break;
		case "SERVICE":
			model.setTemplateName("Service.ftl");
			model.setPreFix("Service");
			model.setSufFix(".java");
			model.setPackageName(".service");
			model.getImportBao().add("import java.util.List");
			model.getImportBao().add("import org.springframework.beans.factory.annotation.Autowired");
			model.getImportBao().add("import org.springframework.stereotype.Service");
			model.getImportBao().add("import com.renbang.model.common.Page");
			model.getImportBao().add("import com.renbang.service.BaseServices");
			break;
		case "ACTION":
			model.setTemplateName("Action.ftl");
			model.setPreFix("Action");
			model.setSufFix(".java");
			model.setPackageName(".action");
			
			model.getImportBao().add("import java.util.List");
			model.getImportBao().add("import javax.servlet.http.HttpServletRequest");
			model.getImportBao().add("import org.springframework.beans.factory.annotation.Autowired");
			model.getImportBao().add("import org.springframework.stereotype.Controller");
			model.getImportBao().add("import org.springframework.web.bind.annotation.RequestMapping");
			model.getImportBao().add("import org.springframework.web.bind.annotation.ResponseBody");
			model.getImportBao().add("import com.renbang.model.common.AjaxResult");
			model.getImportBao().add("import com.renbang.model.common.Page");
			break;
		case "JSPLIST":
			model.setTemplateName("JspList.ftl");
			model.setPreFix("-list");
			model.setSufFix(".jsp");
			model.setPackageName(".views");
			break;
		case "JSPFORM":
			model.setTemplateName("JspForm.ftl");
			model.setPreFix("-form");
			model.setSufFix(".jsp");
			model.setPackageName(".views");
			break;

		}
		return model;
		
	}
	//把字段或表名去下划线转成首大写驼峰
	public static String getBeanName(String tableName){
		StringBuilder BeanName= new StringBuilder(tableName);
		String Fist = (BeanName.charAt(0)+"").toUpperCase();
		BeanName.replace(0, 1, Fist);//把手写字母大写
		for (int i = 0; i < BeanName.length(); i++) {
			if(BeanName.charAt(i)=='_'){
				String Upper = (BeanName.charAt(i+1)+"").toUpperCase();
				BeanName.replace(i, i+2, Upper);
			}
		}
		return BeanName.toString();
	}
	//把字段或表名去下划线转成首小写驼峰
	public static String getPropertyName(String tableName){
		StringBuilder BeanName= new StringBuilder(getBeanName(tableName));
		BeanName.replace(0, 1, (BeanName.charAt(0)+"").toLowerCase());
		return BeanName.toString();
	}
	
	//设置类的属性名，类型和getset方法名
	public static List <Map<String,String>> getPropertys(List <Map<String,String>> COLUMNS){
		 List <Map<String,String>>Propertys = new ArrayList<Map<String,String>>();
		for (Map<String, String> column : COLUMNS) {
			Map<String, String> property = new HashMap<String, String>();
			property.put("DATA_TYPE", column.get("DATA_TYPE"));//数据类型
			property.put("COLUMN_NAME",column.get("COLUMN_NAME"));//字段名
			property.put("PROPERTY", getPropertyName(column.get("COLUMN_NAME")));//对字段名进行格式转换
			property.put("GSET", getBeanName(property.get("PROPERTY")));//getset方法首写字母大写
			Propertys.add(property);
		}
		
		return Propertys;
		
	}
	
	
	@Override
	public String toString() {
		return "MyModel [preFix=" + preFix + ", sufFix=" + sufFix
				+ ", packageName=" + packageName + ", templateName="
				+ templateName + ", property=" + property + ", beanName="
				+ beanName + "]";
	}
	public static void main(String[] args) {
		
		System.out.println(getPropertyName("user_a_b_c"));
	}
}
